package com.uphone.kingmall.adapter;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.china.zhuoyue.R;
import com.luck.picture.lib.config.PictureConfig;
import com.uphone.kingmall.bean.EvaluateBean;
import com.uphone.kingmall.utils.GlideUtils;
import com.uphone.kingmall.utils.PictureSelectUtils;
import com.uphone.kingmall.utils.RatingBar;

import java.util.List;

/**
 * <pre>
 *     author : 孙增艳
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2019/2/27
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class RvEvaluateAdapter extends BaseQuickAdapter<EvaluateBean, BaseViewHolder> {

    private final Activity context;
    private final GoodsEvaluateChange goodsEvaluateChange;

    public RvEvaluateAdapter(Activity context, @Nullable List<EvaluateBean> data, GoodsEvaluateChange goodsEvaluateChange) {
        super(R.layout.kingmall_item_rv_order_evaluate, data);
        this.context = context;
        this.goodsEvaluateChange = goodsEvaluateChange;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final EvaluateBean item) {
        ImageView goodsImage = helper.getView(R.id.iv_goods_image);
        RatingBar ratingBar = helper.getView(R.id.rb_goods);
        EditText etContent = helper.getView(R.id.et_content);
        RecyclerView recyclerView = helper.getView(R.id.rv_upload_image);
        GlideUtils.getInstance().loadCornerImage(context, item.getImageUrl(), SizeUtils.dp2px(5), goodsImage);
        ratingBar.setOnRatingChangeListener(new com.uphone.kingmall.utils.RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {
                goodsEvaluateChange.ratingChange(helper.getAdapterPosition(), ratingCount);
            }
        });

        helper.getView(R.id.iv_upload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                PictureSelectUtils.openMultiplePicture(context, item.getLocalMediaList()
                        , 6, false, 0, 0, PictureConfig.CHOOSE_REQUEST);
                goodsEvaluateChange.imageChange(helper.getAdapterPosition());

            }
        });

        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String s1 = s.toString();
                goodsEvaluateChange.evaluateChange(helper.getAdapterPosition(), s1);
            }
        });
        RvUpLoadImageAdapter upLoadImageAdapter = new RvUpLoadImageAdapter(context, item.getLocalMediaList());
        recyclerView.setLayoutManager(new GridLayoutManager(context, 3));
        recyclerView.setAdapter(upLoadImageAdapter);
        upLoadImageAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                if (position == 0) {
//                }else {
//
//                }
            }
        });
    }

    public interface GoodsEvaluateChange {
        void ratingChange(int position, double rating);

        void evaluateChange(int position, String evaluate);

        void imageChange(int position);
    }
}
